const investSearchModal = require('../models/InvestSearch');

module.exports = function ({ router, baseUrl, type, path, name }) {

    function getRequestOfType (type, keywords) {
        if (keywords) {
            return {
                'invest': () => investSearchModal.searchInvset(keywords),
                'pioneering': () => investSearchModal.searchPioneering(keywords),
            }[type];
        } 
        return {
            'invest': () => investSearchModal.getInvsetList(keywords),
            'pioneering': () => investSearchModal.getPioneeringList(keywords),
        }[type];
    }

    router.get(baseUrl + path, async (ctx, next) => {
        try {
            let keywords = ctx.request.query.keywords;
            let req = getRequestOfType(type, keywords);
            // console.log(req)
            let reses = await Promise.all([
                req()
            ]);
            // console.log(reses[0].list)
            ctx.render('sc-search-result/sc-search-result.pug', { data: reses[0], href: keywords ? `${path}?keywords=${keywords}` : path, name, type, baseUrl, ssr: true });
        } catch (err) {
            // console.log(err)
            next(err);
        }
    });
}